/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import constants from '../../util/Constants';
import { Token } from '../Token';
import { TokenType } from '../TokenType';
export class TokenComment extends Token {
  private _data: string[] = []
  // try to get in one shot
  private dataS: string
  bogus = false;

  constructor() {
    super()
    this.type = TokenType.Comment
  }

  asComment(): TokenComment {
    return this
  }

  reset(): Token {
    super.reset()
    constants.reset(this._data);
    this.dataS = null;
    this.bogus = false;
    return this
  }

  getData(): string {
    return (this.dataS !== undefined && this.dataS !== null) ? this.dataS : this._data.join('')
  }

  append(append: string | number): TokenComment {
    this.ensureData()
    if (typeof append === 'string') {
      if (this._data.length === 0) {
        this.dataS = append
      } else {
        this._data.push(append)
      }
    } else if (typeof append === 'number') {
      this._data.push(String.fromCharCode(append))
    }
    return this
  }

  private ensureData() {
    // if on second hit, we'll need to move to the builder
    if (this.dataS !== undefined && this.dataS !== null) {
      this._data.push(this.dataS);
      this.dataS = null;
    }
  }

  tokenType(): string {
    return "Comment"
  }

  public toString(): string {
    return `<!--${this.getData()}-->`
  }
}